package io.github.shenbinglife.jlock;

import io.github.shenbinglife.jlock.exception.LockFailedException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * LockUtils
 *
 * @author shenbing
 * @date 2022/4/21
 */
public class LockUtils {

  /**
   * Client ID
   * @return  hostName_ProcessName_StartTime_ThreadID
   */
  public static String currentClientId() {
    InetAddress addr = null;
    try {
      addr = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
      throw new LockFailedException("Can not get current hostname for client id");
    }
    String hostName = addr.getHostName();

    return hostName + "_" + ManagementFactory.getRuntimeMXBean().getName() + "_"
        + ManagementFactory.getRuntimeMXBean().getStartTime() + "_"
        + Thread.currentThread().getId();
  }

}
